package com.coollang.smartbra.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.coollang.smartbra.MyAutoLayoutActivity;
import com.coollang.smartbra.R;
import com.coollang.smartbra.base.MyApplication;
import com.coollang.smartbra.constant.Constants;
import com.coollang.smartbra.model.UserModel;
import com.coollang.smartbra.presenter.SplashActivityPresenter;
import com.coollang.smartbra.utils.ActivitySwitcher;
import com.coollang.smartbra.utils.ToastUtils;
import com.coollang.smartbra.utils.Urls;
import com.coollang.smartbra.utils.Utils;

import io.realm.Realm;

/**
 * Splash 页面
 */
public class SplashActivity extends MyAutoLayoutActivity {

    private static final String TAG = "SplashActivity";
    private SplashActivityPresenter mSplashActivityPresenter;
    private View mView;

    @Override
    protected void initView() {
        //无title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_splash);

        mView = findViewById(R.id.rl_con);
    }

    private Handler mHandler = new Handler();

    @Override
    protected void initData() {
        mSplashActivityPresenter = new SplashActivityPresenter(this);

        if (MyApplication.getApplication().hasNet()) {
            mSplashActivityPresenter.getUpdateInfo();
        } else {
            // 没有网络
            goToLoginAct();
        }

    }

    public void isHasNewVersion(boolean hasNewVersion, String downLoadAddress) {
        if (hasNewVersion) {
            showUpdateDialog(downLoadAddress);
        } else {
            // 判断app是否有新版本
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    readCach();
                }
            }, 1500);
        }
    }

    private void readCach() {
        UserModel model = Realm.getDefaultInstance().where(UserModel.class).findFirst();

        if (null != model) {
            // 没有数据
            String username = model.getUsername();
            String password = model.getPassword();
            mSplashActivityPresenter.login(username, password);
        } else {
            goToLoginAct();
        }
    }

    /**
     * 去首页面
     */
    public void goToMainAct() {
        ActivitySwitcher.goMainActivity(this);
        finish();
    }

    /**
     * 去登录页面
     */
    public void goToLoginAct() {
        ActivitySwitcher.goLoginAct(this);
        finish();
    }

    public void onLoginFailed(int errorCode) {
        if (errorCode == Urls.CODE_Error_) {
            ToastUtils.showToast("服务器连接失败!");
        }
        goToLoginAct();
    }

    /**
     * 去欢迎页面
     */
    public void goToWelcomeAct() {
        ActivitySwitcher.goToWelcome2(SplashActivity.this);
        finish();
    }

    private void showUpdateDialog(final String downLoadAddress) {

        // 判断当前app是否有安装应用宝
        final boolean avilible = Utils.isAvilible(this, Constants.QQDOWNLOADER_PKG);

        AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setTitle("升级提示");
        dialog.setMessage("有新版本，是否前往更新？");
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancel1), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                finish();
            }
        });

        dialog.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.to_update), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                if (avilible) {
                    Utils.launchAppDetail(SplashActivity.this, Constants.MYAPP_PKG, Constants.QQDOWNLOADER_PKG);
                } else {
                    gotoUpdate(downLoadAddress);
                }
            }
        });
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);

        dialog.show();

        Button posiviveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
        posiviveButton.setTextColor(Color.parseColor("#F79444"));
        Button negativeButton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
        negativeButton.setTextColor(Color.parseColor("#000000"));
    }

    private void gotoUpdate(String downLoadAddress) {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse(downLoadAddress);
        intent.setData(content_url);
        startActivity(intent);
    }
}
